/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gradius;

import java.awt.Graphics;
import java.awt.Image;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Firedemon
 */
public class Animation 
{
    private List<Image> sprites = new ArrayList<Image>();
    private Graphics g;
    private int x, y;
    private long delay;
    
    private Thread thread;
    
    Animation(Graphics g, List<Image> sprites, int x, int y, long delay)
    {
        this.g       = g;
        this.x       = x;
        this.y       = y;
        this.delay   = delay;
        this.sprites = sprites;
        
        ready();
    }
    
    private void ready()
    {
        Runnable r = new Runnable() 
        {
            @Override
            public void run()
            {
                try
                {
                    for (Image frame : sprites)
                    {
                        g.drawImage(frame, x, y, null);
                        Thread.sleep(delay);
                    }
                } 
                catch (InterruptedException iex) { System.out.println(iex.getMessage()); }
            }
        }; 
        
        thread = new Thread(r);
    }
    
    public void play()
    {
        thread.start();
    }
    
    public void stop()
    {
        thread.interrupt();
    }
}
